<template>
  <div ref="relative" class="dmui-operops-transition-box">
    <div
      class="dmui-operops-transition"
      :class="{
        _show: show,
        _hide: !show,
        _allowUp: allow === 'up'
      }"
      :style="transition"
    >
      <div ref="tran" class="tran">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "tran",
  props: {
    show: {
      type: Boolean,
      default: false
    },
    duration: {
      type: Number | Boolean,
      default: false
    }
  },
  data() {
    return {
      height: 0,
      allow: "down"
    };
  },
  watch: {
    show() {
      this.getTranHeight();
    }
  },
  computed: {
    transition() {
      return {
        height: this.show ? this.height : 0,
        "transition-duration":
          this.duration === false
            ? parseInt(this.height) * 1.2 + 200 + "ms"
            : this.duration + "ms"
      };
    }
  },
  mounted() {
    this.getTranHeight();
  },
  methods: {
    getTranHeight() {
      this.height = window.getComputedStyle(this.$refs.tran).height;
      let offset =
        this.$refs.relative.offsetTop - this.$refs.relative.scrollTop;
      if (window.innerHeight - offset < parseInt(this.height)) {
        this.allow = "up";
      } else {
        this.allow = "down";
      }
    }
  }
};
</script>

<style scoped lang="less">
.dmui-operops-transition-box {
  position: relative;
  z-index: 10;
}
.dmui-operops-transition {
  position: absolute;
  overflow: hidden;
  left: 0;
  width: 100%;
  top: 0;
  opacity: 0;
  padding-top: 2px;
  transform: translateZ(0);
  transition: height 500ms linear, transform 500ms linear,
    opacity 200ms ease-out;
  &._allowUp {
    bottom: 0;
    top: unset;
  }

  &._show {
    opacity: 1;
    transform: translateZ(0);
    transition: height 400ms linear, transform 200ms linear,
      opacity 500ms ease-in;
  }
}
.tran {
  position: absolute;
  left: 0;
  width: 100%;
  bottom: 0;
}
</style>
